"""--------------------------------------------------------------------------------------------------
Filename:   dialogs.py
Author:     Chris Chester (christopher.chester@pg.canterbury.ac.nz) adapted from work done by Ben Han
Date:       04 September 2012
Description:
    Contains classes to display menus or dialogs.
--------------------------------------------------------------------------------------------------"""

import wx
import time
import splashLogo
      
#===============================================================================
#    CLASS: BreakDialog
#    Creates a frame for the break timer
#===============================================================================
class BreakDialog(wx.Frame):
    """ This class implements a basic timer with an elapsed time display.
        The constructor takes a minimum of two arguments:
            - a callable object to be run when the timer reaches the specified time
            - the length of time before running callable object (seconds)
        This
        is done when the button is clicked, and also when the window is closed. So
        every case leads to the execution of the _end_timer() function, which
        closes the window.
    """
    def __init__(self, on_timer, period, parent=None, id=-1):
        wx.Frame.__init__(self, parent, id, title='Take a break :-)', size=(300, 150))
        self.on_timer = on_timer
        self._break_len = period
        
        self.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.BOLD))
        panel = wx.Panel(self, -1)
        self.break_button = wx.Button(panel, -1, 'Continue trials', size=(150, 30))
        self.time_display = wx.TextCtrl(panel, -1, size=(200, 30), style=wx.TE_CENTRE)
        self.time_display.SetEditable(False)
        
        box = wx.StaticBox(panel, -1, "Break Timer")
        sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
        sizer.Add(self.time_display, flag=wx.ALIGN_CENTER | wx.ALL, border=5)
        sizer.Add(self.break_button, flag=wx.ALIGN_CENTER | wx.ALL, border=5)
        panel.SetSizer(sizer)

        self._update_time(None)
        self.timer = wx.Timer(self, -1)
        self.timer.Start(1000)      # update timer every second (1000ms)
        self.Bind(wx.EVT_TIMER, self._update_time)
        self.Bind(wx.EVT_CLOSE, self._on_close)
        self.Bind(wx.EVT_BUTTON, self._end_timer, self.break_button)
        self.Centre()
        self.MakeModal(True)        # must be set to False before closing this window
        self.Show()
        
    def _update_time(self, event):
        # Decrement timer
        mins = 0
        secs = self._break_len
        if self._break_len >= 60:
            mins = int(self._break_len / 60)
            secs = self._break_len - (60 * mins)
        if secs < 10:
            secs = '0' + str(secs)
        time_str = '%i:%s' % (mins, secs)
        self.time_display.SetValue(time_str)
        self.Refresh()
        if self._break_len <= 0:
            self._end_timer(None)
        self._break_len -= 1

    def _end_timer(self, event):
        self.on_timer()
        self.MakeModal(False)
        self.Destroy()
        
    def _on_close(self, event):
        self._end_timer(event)


#===============================================================================
# CLASS: AboutDialog
##    Dialog displaying information about the application
#===============================================================================
class AboutDialog(wx.MessageDialog):        
    def __init__(self, parent):
        about_text = """Biofeedback in Swallowing Skill Training (BiSSkiT)
Version:     0.816
Released:    16 August 2010

This application was designed for use with the 
MyoPace sEMG biofeedback device by Niche Technology.
Electrode pair should be placed over the sub-mental
muscles (with nearby reference).

The software allows the user to undertake the
traditional strength training approach or an 
alternative skill training approach which
emphasises the accuracy and precision of muscle
contractions used in swallowing. 

Author:
    Ben Han (uha10@uclive.ac.nz)
    Oshrat Sella
    Maggie-lee Huckabee 
    Richard Jones
    Paul Gaynor
   (Van der Veer Institute for 
    Parkinson's and Brain Research
    Christchurch, NZ)
"""
        wx.MessageDialog.__init__(self, parent,
                                  about_text,
                                  caption="About", 
                                  style=wx.OK|wx.CENTRE)
        
        self.ShowModal()



#===============================================================================
# CLASS: StartDialog
##    A splash screen to be displayed before launching the application.
#===============================================================================
class StartDialog(wx.SplashScreen):
    """
Create a splash screen widget.
    """
    def __init__(self, parent=None):
      
        logo = splashLogo.getsplashBitmap()
        splashStyle = wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT
        splashDuration = 2000 # milliseconds
        # Call the constructor with the above arguments in exactly the
        # following order.
        wx.SplashScreen.__init__(self, logo, splashStyle, splashDuration, parent)
        wx.Yield()
        
# End of dialogs.py
